{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.cluster import DBSCAN\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 载入数据\n",
    "data = np.genfromtxt(\"kmeans.txt\", delimiter=\" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DBSCAN(algorithm='auto', eps=1, leaf_size=30, metric='euclidean',\n",
       "    min_samples=4, n_jobs=1, p=None)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 训练模型\n",
    "# eps距离阈值，min_samples核心对象在eps领域的样本数阈值\n",
    "model = DBSCAN(eps=1, min_samples=4)\n",
    "model.fit(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  4,  2, -1, -1, -1,  2,  0,  1,  3,  2,  0,  1, -1, -1,  0,\n",
       "        1,  3,  2,  0,  1,  3,  2,  0,  1,  3,  2,  0, -1, -1,  2,  0, -1,\n",
       "        4,  2,  0,  1,  3,  2,  0,  1,  3,  2,  0,  1,  3,  2,  0,  1,  3,\n",
       "        2, -1, -1,  3,  2,  0,  1,  3,  2,  0,  1,  3,  2, -1,  1,  3,  2,\n",
       "        0,  1,  3,  2, -1,  1,  4,  2,  0,  1,  4,  2], dtype=int64)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = model.fit_predict(data)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png":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Z3v5B1e+JmZq/SaFL4dkm/kPR9SX/j7s9l2rCE2KhH8oik37n7Q1m3Jy2Xeuwfcfe7sm7L\nLBhjR1w+K+k1kh4zs6fN7AtZNyhOlyaGPyTpWxpNJP5rqKF+ya2S7pP09ks/z6cv9WhRAPTYASAw\n9NgBIDAEOwAEhmAHgMAQ7AAQGIIdAAJDsANAYAh2AAgMwQ4Agfl/LB6Ron2T0KUAAAAASUVORK5C\nYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2a7d4ec4cf8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画出各个数据点，用不同颜色表示分类\n",
    "mark = ['or', 'ob', 'og', 'oy', 'ok', 'om']\n",
    "for i,d in enumerate(data):\n",
    "    plt.plot(d[0], d[1], mark[result[i]])\n",
    "    \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
